import Vue from 'vue'
import VueRouter from 'vue-router'
import Detail from '../views/detail/index.vue'
import Footer from '../components/footer.vue'
import SearchRes from '../views/searchRes/index.vue'
import BrandList from '../views/brandList/index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    components: {
      default: () => import(/* webpackChunkName: "home" */ '../views/home/index.vue'),
      footer: Footer
    }
  }, {
    path: '/kind',
    name: 'Kind',
    components: {
      default: () => import(/* webpackChunkName: "home" */ '../views/kind/index.vue'),
      footer: Footer
    }
  }, {
    path: '/cart',
    name: 'Cart',
    components: {
      default: () => import(/* webpackChunkName: "home" */ '../views/cart/index.vue')
    }
    // beforeEnter (to, from, next) {
    //   if (localStorage.getItem('isLogin') === 'ok') {
    //     next()
    //   } else {
    //     next('/login')
    //   }
    // }
  },
  {
    path: '/my',
    name: 'My',
    components: {
      default: () => import(/* webpackChunkName: "home" */ '../views/my/index.vue'),
      footer: Footer
    },
    beforeEnter (to, from, next) {
      if (localStorage.getItem('isLogin') === 'ok') {
        next()
      } else {
        next('/login?t=' + to.path)
      }
    }
  },
  {
    path: '/setUserMsg',
    components: {
      default: () => import(/* webpackChunkName: "home" */ '../views/setUser/index.vue')
    },
    beforeEnter (to, from, next) {
      if (localStorage.getItem('isLogin') === 'ok') {
        next()
      } else {
        next('/login?t=' + to.path)
      }
    }
  },
  // {
  //   path: '/detail/:id',
  //   name: 'detail',
  //   components: {
  //     default: Detail
  //   }
  // },
  {
    path: '/detail/:id',
    name: 'detail',
    components: {
      default: Detail
    },
    props: {
      default: true
    }
  },
  {
    path: '/register',
    name: 'register',
    components: {
      default: () => import(/* webpackChunkName: "home" */ '../views/register/index.vue')
    }
  },
  {
    path: '/login',
    name: 'login',
    components: {
      default: () => import(/* webpackChunkName: "home" */ '../views/login/index.vue')
    }
  },
  {
    path: '/order',
    name: 'order',
    components: {
      default: () => import(/* webpackChunkName: "home" */ '../views/order/index.vue')
    }
  },
  {
    path: '/orderDetail/:id',
    name: 'orderDetail',
    components: {
      default: () => import(/* webpackChunkName: "home" */ '../views/orderDetail/index.vue')
    },
    props: {
      default: true
    }
  },
  {
    path: '/search',
    name: 'search',
    components: {
      default: () => import(/* webpackChunkName: "home" */ '../views/search/index.vue')
    }
  },
  {
    path: '/searchRes/:id',
    name: 'searchRes',
    component: SearchRes,
    props: true
  },
  {
    path: '/brandList',
    name: 'brandList',
    component: BrandList
  }
]
// 动态路由传递数据的方式
// params
// 路由的定义格式：path: '/detail/:id'
// 路由的跳转格式：/detail/jagduy1uy1f
// http://localhost:8080/detail/pro44172272-0ead-11eb-8223-5d4b2a238f09
// 解析：$router.params.id
// query
// 路由的定义格式：path: '/detail'
// 路由的跳转格式：/detail?id=jagduy1uy1f
// http://localhost:8080/detail/?id=pro44172273-0ead-11eb-8223-5d4b2a238f09
// 解析：$router.query.id
// 路由的跳转方式
// 标签跳转：router-link
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局的路由守卫：不推荐使用
// router.beforeEach((to, from, next) => {
//   // to 要跳转的路由对象
//   console.log(to)
//   // from 来源路由对象
//   // next 跳转的回调函数
//   // next('/login')
//   // next()
//   if (to.path === '/cart' && localStorage.getItem('isLogin') !== 'ok') {
//     next('/login')
//   } else {
//     next()
//   }
// })

export default router
